// 题意：和求树上是否有一个点对距离等于k
//
// 题解：点分治，找到重心分治，然后将所有点到重心的距离排序，O(n)判断是否有
//       点对为k。复杂度 O(n*log(n)*log(n))
//       给定n个排好序的个数，统计恰好为k的个数有点trick，特别要注意当有
//       一些列为k/2, k/2, k/2, k/2...的数的时候要将右指针重置到当轮初始
//       位置。
//       fuck poj, 本来open judge过了。。poj还一直TLE，把tmp_count移到全局
//       就过了，，poj开优化会死啊？？
// 
// run: time $exec < input > output
#include <cstdio>
#include <algorithm>
#include <vector>
#include <utility>

int const inf = 1 << 28;
int const maxn = 10007;
bool centroid[maxn];
int subtree_size[maxn];
int n, k;

typedef std::pair<int, int> node_type;
typedef std::vector<node_type> adj_edge;
typedef adj_edge::iterator edge_iter;


//#define RAW_GRAPH
#define HUGE_INPUT

#ifdef RAW_GRAPH
// assume one node's maximum edge is 200.
node_type graph[maxn][200];
#else
adj_edge graph[maxn];
#endif


std::vector<int> count;
std::vector<int> tmp_count;

int calc_subtree_size(int u, int parent)
{
	int sum = 1;

#ifdef RAW_GRAPH
	for (int i = 1; i <= graph[u][0].first; i++) {
		int v = graph[u][i].first;
#else
	for (int i = 0; i < (int)graph[u].size(); i++) {
		int v = graph[u][i].first;
#endif
	//for (edge_iter it = graph[u].begin(); it != graph[u].end(); ++it) {
	//	int v = it->first;
		if (centroid[v] || parent == v) continue;
		sum += calc_subtree_size(v, u);
	}
	return subtree_size[u] = sum;
}

std::pair<int, int> get_center(int u, int parent, int all)
{
	std::pair<int, int> center(inf, -1);
	int sum = 1, max_sub = 0;

#ifdef RAW_GRAPH
	for (int i = 1; i <= graph[u][0].first; i++) {
		int v = graph[u][i].first;
#else
	for (int i = 0; i < (int)graph[u].size(); i++) {
		int v = graph[u][i].first;
#endif
	//for (edge_iter it = graph[u].begin(); it != graph[u].end(); ++it) {
	//	int v = it->first;
		if (centroid[v] || parent == v) continue;
		max_sub = std::max(max_sub, subtree_size[v]);
		sum += subtree_size[v];
		center = std::min(center, get_center(v, u, all));
	}
	max_sub = std::max(max_sub, all - sum);
	return std::min(center, std::make_pair(max_sub, u));
}

void calc_dis_to_center(int u, int parent, int dis)
{
	tmp_count.push_back(dis);

#ifdef RAW_GRAPH
	for (int i = 1; i <= graph[u][0].first; i++) {
		int v = graph[u][i].first, c = graph[u][i].second;
#else
	for (int i = 0; i < (int)graph[u].size(); i++) {
		int v = graph[u][i].first, c = graph[u][i].second;
#endif
	//for (edge_iter it = graph[u].begin(); it != graph[u].end(); ++it) {
	//	int v = it->first, c = it->second;
		if (centroid[v] || parent == v) continue;
		calc_dis_to_center(v, u, dis + c);
	}
}

template <class T>
int max_bit(std::vector<T> const & a)
{
	int max_data = a[0];
	for (int i = 1; i < (int)a.size(); i++)
		max_data = std::max(max_data, a[i]);
	int d = 1;
	for (int p = 10; max_data >= p; p *= 10) d++;
	return d;
}

template <class T>
void radix_sort(std::vector<T> & a)
{
	int mbit = max_bit(a);
	int n = a.size();
	std::vector<T> tmp(n);
	std::vector<int> count(10);
	for (int i = 1, radix = 1; i <= mbit; i++, radix *= 10, a = tmp) {
		std::fill(count.begin(), count.end(), 0);
		for (int j = 0; j < n; j++) {
			int k = (a[j] / radix) % 10;
			count[k]++;
		}
		for (int j = 1; j < 10; j++)
			count[j] = count[j - 1] + count[j];
		for (int j = n - 1; j >= 0; j--) {
			int k = (a[j] / radix) % 10;
			tmp[count[k] - 1] = a[j];
			count[k]--;
		}
	}
}

int calc_pair(std::vector<int> & count)
{
	std::sort(count.begin(), count.end());
	//radix_sort(count);
	int len = count.size(), tot = 0;
	for (int l = 0, r = len - 1; l < r; l++) {
		for (; l < r && count[l] + count[r] > k; r--);
		if (count[l] + count[r] == k) {
			int tr = r;
			for (; l < r && count[l] + count[r] == k; r--);
			r++;
			int tl = l;
			for (; l < r && count[l] + count[r] == k; l++);
			r--;
			tot += (tr - r) * (l - tl);
			if (count[r+1] * 2 == k) {
				int t = tr - r;
				tot += t * (t + 1) / 2;
			}
			l--;
		}
	}
	return tot;
}

bool divide_and_conquer_for_tree(int u)
{
	calc_subtree_size(u, -1);
	int center = get_center(u, -1, subtree_size[u]).second;
	centroid[center] = true;

#ifdef RAW_GRAPH
	for (int i = 1; i <= graph[center][0].first; i++) {
		int v = graph[center][i].first;
#else
	for (int i = 0; i < (int)graph[center].size(); i++) {
		int v = graph[center][i].first;
#endif
	//for (edge_iter it = graph[center].begin(); it != graph[center].end(); ++it) {
	//	int v = it->first;
		if (centroid[v]) continue;
		if (divide_and_conquer_for_tree(v)) {
			centroid[center] = false;
			return true;
		}
	}

	count.clear();
	count.push_back(0);
	int tmp = 0;

#ifdef RAW_GRAPH
	for (int i = 1; i <= graph[center][0].first; i++) {
		int v = graph[center][i].first, c = graph[center][i].second;
#else
	for (int i = 0; i < (int)graph[center].size(); i++) {
		int v = graph[center][i].first, c = graph[center][i].second;
#endif
	//for (edge_iter it = graph[center].begin(); it != graph[center].end(); ++it) {
	//	int v = it->first, c = it->second;
		if (centroid[v]) continue;
		tmp_count.clear();
		calc_dis_to_center(v, center, c);
		tmp -= calc_pair(tmp_count);
		count.insert(count.end(), tmp_count.begin(), tmp_count.end());
	}
	tmp += calc_pair(count);
	centroid[center] = false;
	return tmp > 0;
}

void scan(int & a)
{
	a = 0;
	bool flag = false;
	char ch = std::getchar();
	if (ch == '-')
		flag = true;
	else if (ch >= '0' && ch <= '9')
		a = ch - '0';

	while ((ch = std::getchar()) >= '0' && ch <= '9')
		a = a * 10 + ch - '0';
	if (flag) a = -a;
}


int main()
{
	/*
	std::vector<int> v{4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 6};
	k = 10;
	std::printf("%d\n", calc_pair(v));
	return 0;
	*/
#ifdef HUGE_INPUT
	while (scan(n), n) {
#else
	while (std::scanf("%d", &n) && n) {
#endif


#ifdef RAW_GRAPH
		for (int i = 0; i < n; i++) graph[i][0].first = 0;
#else
		for (int i = 0; i < n; i++) graph[i].clear();
#endif

		for (int i = 0; i < n; i++) {
			node_type tmp;

#ifdef HUGE_INPUT
			while (scan(tmp.first), tmp.first) {
#else
			while (std::scanf("%d", &tmp.first) && tmp.first) {
#endif

				tmp.first--;


#ifdef HUGE_INPUT
				scan(tmp.second);
#else
				std::scanf("%d", &tmp.second);
#endif


#ifdef RAW_GRAPH
				graph[i][++graph[i][0].first] = tmp;
				graph[tmp.first][++graph[tmp.first][0].first] = std::make_pair(i, tmp.second);
#else
				graph[i].push_back(tmp);
				graph[tmp.first].push_back(std::make_pair(i, tmp.second));
#endif
			}
		}

#ifdef HUGE_INPUT
		for (; scan(k), k; ) {
#else
		for (; std::scanf("%d", &k) && k; ) {
#endif
			if (divide_and_conquer_for_tree(0))
				std::printf("AYE\n");
			else
				std::printf("NAY\n");
		}

		std::printf(".\n");
	}
}

